using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RapidHDL
{
    public partial class IO : UserControl
    {
        RapidHardware RapidHardware;
        
        string sSignalName = "";
        bool bSource = false ;
        bool bView = false;

        SignalSource oSource = null;
        SignalView oView = null;

        public IO()
        {
            InitializeComponent();
        }

        private void IO_Load(object sender, EventArgs e)
        {
            
        }

        public void AssignSignal(RapidHardware poRapidHardware, string psSignalName, bool pbSource, bool pbView)
        {
            RapidHardware = poRapidHardware;
            sSignalName = psSignalName;
            bSource = pbSource;
            bView = pbView;
            ArrangeDisplay();
            RapidHardware.RapidHardwareEvent += new RapidHardware.RapidHardwareEventDelegate(this.RapidHardwareEvent);
        }

        private void ArrangeDisplay()
        {
            lblSignalName.Text = sSignalName;
            if (bSource)
            {
                txtInput.Visible = true;
                cmdWrite.Visible = true;
            }
            else
            {
                txtInput.Visible = false;
                cmdWrite.Visible = false;
            }
            lblValue.Text = "??";
            if (bView)
                cmdRead.Visible = true;
            else
                cmdRead.Visible = false;
        }

        private void RefreshValue()
        {
            if (sSignalName == "" || oView == null) return;
            lblValue.Text = oView.ValueAsString;
        }

        bool RapidHardwareEvent(RapidHardware poRapidHardware, RapidHardwareEventTypes pEventType, object oParam1, object oParam2)
        {
            switch (pEventType)
            {
                case RapidHardwareEventTypes.AfterTest:
                    GetSignalObjects();
                    break;
                case RapidHardwareEventTypes.AfterHardwareExecute:
                    RefreshValue();
                    break;
                case RapidHardwareEventTypes.HardwareStarted:
                    RefreshValue();
                    break;
                default:
                    break;
            }
            return true;
        }

        private void GetSignalObjects()
        {
            if (sSignalName == "")
                return;

            if (bSource)
                oSource = RapidHardware.SystemInterface.GetSignalSource(sSignalName);

            if (bView)
                oView = RapidHardware.SystemInterface.GetSignalView(sSignalName);
        }

        private void cmdRead_Click(object sender, EventArgs e)
        {
            RefreshValue();
        }

        private void cmdWrite_Click(object sender, EventArgs e)
        {
            if (sSignalName == "" || oSource == null) return;
            if (!Conversion.IsNumeric(txtInput.Text))
            {
                lblValue.Text = "Non-Numeric Input";
                return;
            }
            oSource.ValueAsInt = Conversion.StringToInt(txtInput.Text);
            lblValue.Text = oSource.ValueAsString;             
        }


    }
}
